//
//  LHTouristDetailUserView.m
//  Knight_Tour
//
//  Created by dlios on 15-5-15.
//  Copyright (c) 2015年 TouchOne. All rights reserved.
//

#import "LHTouristDetailUserView.h"
#import "LHDetailModel.h"

@implementation LHTouristDetailUserView
- (void)dealloc
{
    [_modelArr release];
    [_tableView release];
    [super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, LHWidth, LHheight - 64) style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"12"]];
        _tableView.alpha = 0.7;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [[UIView alloc] init];
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"user"];
        [self addSubview:_tableView];
        [_tableView release];
    }
    return self;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"user"];
    cell.textLabel.text = [_modelArr[indexPath.row] titleName];
//    cell.contentView.alpha = 0.7;
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _modelArr.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self.delegate jumpToCommon:indexPath];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //创建文件管理器
        NSFileManager *manager = [NSFileManager defaultManager];
        //获取路径
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        //plist路径
        NSString *pathStr = [[path stringByAppendingPathComponent:@"LHCountrySightDetail"] stringByAppendingPathComponent:@"LHCountrySightDetail.kk"];
        [self.modelArr removeObjectAtIndex:indexPath.row];
        [NSKeyedArchiver archiveRootObject:self.modelArr toFile:pathStr];
        [_tableView reloadData];
        if (self.modelArr.count == 0) {
            [manager removeItemAtPath:pathStr error:nil];
        }
    }
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

@end
